package com.hilaver.dzmis.web.servlet.product;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductWorkFlow;
import com.hilaver.dzmis.service.impl.ProductTimeLineServiceImpl;
import com.hilaver.dzmis.service.impl.ProductWorkFlowServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class ProductWorkFlowServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	private ProductWorkFlowServiceImpl pwfService;
	
	private ProductWorkFlow pwf;

	public ProductWorkFlowServlet() {
		super();
		this.simpleName = ProductWorkFlow.class.getSimpleName();
		this.fullName = ProductWorkFlow.class.getName();
		this.pwfService = new ProductWorkFlowServiceImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.pwfService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.pwfService);
		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			rtn = this.pwfService.edit(this.pwf);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.pwfService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.pwfService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setId(request);
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = this.pwfService.getAllPagination(this.page,
					this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_ALL:
			
			break;
		}

		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.pwf = (ProductWorkFlow)this.obj;
		
		Integer piId = Integer.parseInt(request.getParameter("piId"));
		ProductIdentification pi = new ProductIdentification();
		pi.setId(piId);
		this.pwf.setProductIdentification(pi);
	}
	
}
